package com.ruoyi.source.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Random;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.SourceBaseInfoMapper;
import com.ruoyi.db.domain.SourceBaseInfo;
import com.ruoyi.source.service.ISourceBaseInfoService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 来源基本表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-26
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SourceBaseInfoServiceImpl implements ISourceBaseInfoService 
{
    @Resource
    private SourceBaseInfoMapper sourceBaseInfoMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询来源基本表
     * 
     * @param id 来源基本表主键
     * @return 来源基本表
     */
    @Override
    public SourceBaseInfo selectSourceBaseInfoById(Long id)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoById(id);
    }

    /**
     * 查询来源基本表列表
     * 
     * @param sourceBaseInfo 来源基本表
     * @return 来源基本表
     */
    @Override
    public List<SourceBaseInfo> selectSourceBaseInfoList(SourceBaseInfo sourceBaseInfo)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoList(sourceBaseInfo);
    }

    /**
     * 查询来源基本表列表
     *
     * @param sourceBaseInfo 来源基本表
     * @return 来源基本表
     */
    @Override
    public List<Map<String, Object>> selectSourceBaseInfoListMap(SourceBaseInfo sourceBaseInfo)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoListMap(sourceBaseInfo);
    }

    /**
     * 新增来源基本表
     * 
     * @param sourceBaseInfo 来源基本表
     * @return 结果
     */
    @Override
    public int insertSourceBaseInfo(SourceBaseInfo sourceBaseInfo)
    {
        String s = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER, 16);
        String s1 = RandomUtil.randomString(RandomUtil.BASE_CHAR.toUpperCase(), 32);
        sourceBaseInfo.setCreateTime(DateUtils.getNowDate());
        sourceBaseInfo.setAppKey(s);
        sourceBaseInfo.setAppSecret(s1);
        return sourceBaseInfoMapper.insertSourceBaseInfo(sourceBaseInfo);
    }

    /**
     * 修改来源基本表
     * 
     * @param sourceBaseInfo 来源基本表
     * @return 结果
     */
    @Override
    public int updateSourceBaseInfo(SourceBaseInfo sourceBaseInfo)
    {
        sourceBaseInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sourceBaseInfoMapper.updateSourceBaseInfo(sourceBaseInfo);
        if (i > 0){
            // 删除线索配置信息
            String redisKey = "SOURCEIFNO:CODE:"+sourceBaseInfo.getChannelCode();
            redisTemplate.delete(redisKey);
        }

        return i;
    }

    /**
     * 批量删除来源基本表
     * 
     * @param ids 需要删除的来源基本表主键
     * @return 结果
     */
    @Override
    public int deleteSourceBaseInfoByIds(String ids)
    {
        return sourceBaseInfoMapper.deleteSourceBaseInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除来源基本表信息
     * 
     * @param id 来源基本表主键
     * @return 结果
     */
    @Override
    public int deleteSourceBaseInfoById(Long id)
    {
        return sourceBaseInfoMapper.deleteSourceBaseInfoById(id);
    }

    @Override
    public List<Map<String, Object>> selectSimpleDataList() {
        return sourceBaseInfoMapper.selectSimpleDataList();
    }

    @Override
    public  String selectChannelCode(String channelCode) {
        return sourceBaseInfoMapper.selectChannelCode(channelCode);
    }


}
